package com.uottawa.clickerapp.views;

import java.util.List;

import com.uottawa.clickerapp.R;
import com.uottawa.clickerapp.service.ClickerAppWebServiceClient;
import com.uottawa.clickerapp.service.impl.ClickerAppWebServiceClientImpl;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SessionList extends Activity implements OnItemClickListener {
	List<String> names;
	ListView listView;
	TextView textView;
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        new Thread(new Runnable() {
            public void run() {
            	ClickerAppWebServiceClient client =	ClickerAppWebServiceClientImpl.getInstance();
                names = client.getRecentSessions();
            }
          }).start();
        try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        setContentView(R.layout.rowlayout);
        textView = (TextView) findViewById(R.id.thetitle);
        textView.setText("Please Select a Session");
        listView = (ListView) findViewById(R.id.listnames);
        if (names != null)
        {
	        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, names);
	        listView.setAdapter(arrayAdapter);
        }
        listView.setOnItemClickListener(this);
  }
  @Override
    public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
	String selection = (String) ((TextView) v).getText();
	Intent goQuestion = new Intent(SessionList.this,QuestionList.class);
	goQuestion.putExtra("SessionSelected", selection);
	
	startActivity(goQuestion);
  }
} 